package User;

import Book.Book;
import Book.Library;
import Utils.PermissionException;
import Utils.ScannerSingleton;

import java.util.Scanner;

import static constant.constant.*;


public class ProxyUser {
    public String name;
    public int userid;
    public Scanner scanner;
    private User ProxyUser;
//    private Library library = new Library();
    private Library library;

    public ProxyUser(User user) {
        ProxyUser = user;
        library = Library.getLibrary();
    }
    public User getProxyUser() {
        return ProxyUser;
    }
    //管理员操作
    private void checkPermission(String message) {
        if (!(this.ProxyUser instanceof AdminUser)) {
            throw new PermissionException(message);
        }
    }
    private void checkPermissionNOR(String message) {
        if (!(this.ProxyUser instanceof NormalUser)) {
            throw new PermissionException(message);
        }
    }
    public int display() {
        return this.ProxyUser.display();
    }
    //上架图书
    public void addBook() {
        System.out.println("执行ProxyUser上架图书的方法");
        try{
            checkPermission("只有管理员才能执行此操作");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.ProxyUser)).addBook();
    }
    //图书修改⽀持修改书名作者类别=
    public void updateBook() {
        try{
            checkPermission("只有管理员才能执行此操作");
        }catch (PermissionException e){
            e.printStackTrace();
        }
     ((AdminUser)ProxyUser).updateBook();
    }
    //删除书籍
    public void removeBook() {
        System.out.println("执行ProxyUser删除图书的方法");
        try{
            checkPermission("只有管理员才能执行此操作");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)ProxyUser).removeBook();
    }
    //统计每本书的借阅次数
    public void borrowCount() {
        System.out.println("执行ProxyUser图书借阅次数的方法");
        try{
            checkPermission("只有管理员才能执行此操作");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)ProxyUser).borrowCount();
    }
    //查询最受欢迎的前 K 本书
    public void generateBook() {
        System.out.println("执行ProxyUser生成最受欢迎的前 K 本书的方法");
        try{
            checkPermission("只有管理员才能执行此操作");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)ProxyUser).generateBook();
    }
    //查看库存状态
    public void checkInventoryStatus() {
        System.out.println("执行ProxyUser查看库存状态的方法");
        try{
            checkPermission("只有管理员才能执行此操作");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)ProxyUser).checkInventoryStatus();
    }
    //移除上架超过⼀年的图书
    public void checkAndRemoveOldBooks() {
        System.out.println("执行ProxyUser移除上架超过⼀年的图书的方法");
        try{
            checkPermission("只有管理员才能执行此操作");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)ProxyUser).checkAndRemoveOldBooks();
    }
    //普通用户
    //借阅图书
    public void borrowBook() {
        System.out.println("执行ProxyUser借阅图书的方法");
        checkPermissionNOR("只有普通用户才能执行此操作");
        ((NormalUser)ProxyUser).borrowBook();
    }
    //归还图书
    public void returnBook() {
        System.out.println("执行ProxyUser归还图书的方法");
        checkPermissionNOR("只有普通用户才能执行此操作");
        ((NormalUser)ProxyUser).returnBook();
    }
    //查看个⼈借阅情况
    public void viewBorrowBooks() {
        System.out.println("执行ProxyUser查看个⼈借阅情况的方法");
        checkPermissionNOR("只有普通用户才能执行此操作");
        ((NormalUser)ProxyUser).viewBorrowBooks();
    }
    public void handleoperation(int choice){
        if (this.ProxyUser instanceof AdminUser){
            switch (choice) {
                case SEARCH_BOOK:
                    this.library.searchBook();
                    break;
                case DISPLAY_BOOK:
                    this.library.displaybook();
                    break;
                case EXIT:
                    this.library.exit();
                    break;
                case ADD_BOOK:
                    addBook();
                    break;
                case UPDATE_BOOK:
                    updateBook();
                    break;
                case REMOVE_BOOK:
                    removeBook();
                    break;
                case BORROW_COUNT:
                    borrowCount();
                    break;
                case GENERATE_BOOK:
                    generateBook();
                    break;
                case CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case CHECK_AND_REMOVE_OLD_BOOKS:
                    checkAndRemoveOldBooks();
                    break;
                default:
                    break;
        }
        }else{
            switch (choice) {
                case SEARCH_BOOK:
                    library.searchBook();
                    break;
                case DISPLAY_BOOK:
                    library.displaybook();
                    break;
                case EXIT:
                    library.exit();
                    break;
                case BORROW_BOOK:
                    borrowBook();
                    break;
                case RETURN_BOOK:
                    returnBook();
                    break;
                case VIEW_BORROW_BOOKS:
                    viewBorrowBooks();
                    break;
                default:
                    break;
            }
        }
    }
}

